﻿using SmartSapce.ServiceLocation;
using SmartSpace.Web;
using SmartSpace.Workflow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp.Modules.Workflow
{
    public partial class Activity_List : SsTemplatePage
    {
        public Guid WorkflowId{get;private set;}
        public Guid ActivityId { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            WorkflowId = RequestUtility.GetGuid(Request, "wfid");
            ActivityId = RequestUtility.GetGuid(Request, "actid");

            _ctlLinkCreate.NavigateUrl = string.Format("~/Modules/Workflow/Pages/Activity-Create.aspx?wfid={0}", WorkflowId);

            if (!IsPostBack)
            {
                IWorkflowService service = ServiceLocator.Get<IWorkflowService>();
                ListResult<ActivityMetadata> activities = service.GetActivities(WorkflowId);

                _ctlList.DataSource = activities.Data;
                _ctlList.DataBind();

                if (ActivityId != Guid.Empty)
                {
                    _ctlLinkCreateEvent.NavigateUrl = string.Format("Event-Create.aspx?wfid={0}&actid={1}", WorkflowId, ActivityId);
                    ListResult<EventMetadata> events = service.GetEvents(ActivityId);
                    _ctlListEvent.DataSource = events.Data;
                    _ctlListEvent.DataBind();
                }
            }
        }

        protected string GetActivityCaption(Guid activityId)
        {
            IWorkflowService service = ServiceLocator.Get<IWorkflowService>();
            return service.GetActivity(activityId).Data.Caption;
        }
    }
}